#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string>
#include<vector>
#include<stdio.h>

using namespace std;

#define num 4



int Ticket=100;

struct threadData{
    string threadName;
};

void* getTicket(void*args)
{
    struct threadData*td=(struct threadData*)args;
    //cout<<td->threadName<<endl;
    const char* name=(td->threadName).c_str();
    
    while(1)
    {
        if(Ticket>0)
        {
            usleep(100000);
            printf("I am %s;get a ticket %d\n",name,Ticket);
            Ticket--;
        }
        else
        {
            break;
        }
    }

    return nullptr;
}

int main()
{

    vector<pthread_t> tids;
    vector<struct threadData*> tds;

    for(int i=1;i<=num;i++)
    {
        //struct threadData td;栈区空间
        struct threadData * td=new threadData;//堆区空间
        pthread_t tid;
        td->threadName="thread-"+to_string(i);
        tds.push_back(td);
        //cout<<tds[i-1]->threadName<<endl;
        pthread_create(&tid,nullptr,getTicket,tds[i-1]);
        tids.push_back(tid);
    }

    for(auto thread:tids)
    {
        pthread_join(thread,nullptr);
    }

    for(auto e:tds)
    {
        delete e;
    }

    return 0;
}